package summary;

import java.util.Arrays;
import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/7/6 21:09
 * https://leetcode.cn/problems/next-greater-element-ii/
 */
public class Title503 {

    public static int[] nextGreaterElements(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        Arrays.fill(result,-1);
        Stack<Integer> stack = new Stack<>();
        int count = 0;
        for (int i = 0; i < n * 2; i++) {
            while (!stack.isEmpty() && nums[i % n] > nums[stack.peek()]) {
                Integer left = stack.pop();
                if (result[left] == -1) {
                    result[left] = nums[i % n];
                }
            }
            stack.push(i % n);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 3};
        nextGreaterElements(arr);
    }
}
